# encoding:utf-8
# !@Time:2022/3/2 20:17
# !@Author:zy
import pyquery
import re


def tomcat():
    doc = pyquery.PyQuery(url='http://archive.apache.org/dist/tomcat/')
    lis = doc('pre').children('a').items()
    version = {}
    for node in lis:
        path = node.attr.href
        if str(path).startswith('tomcat-'):
            doc1 = pyquery.PyQuery(url='http://archive.apache.org/dist/tomcat/{}'.format(path))
            index = [str(path + i.attr.href) for i in doc1('pre').children('a').items() if str(i.attr.href).startswith('v')]
            d = {path: index}
            version.update(d)
    return version


def xstream():
    doc = pyquery.PyQuery(url='http://x-stream.github.io/download.html')
    version = doc('h1').children('span').text()

    return dict({'xstream': version})


def ApacheShiro():
    pattern = re.compile(r'.*Apache Shiro.*?is the current stable release')
    doc = pyquery.PyQuery(url='https://shiro.apache.org/download.html')
    version = doc('.paragraph').children('p').items()

    for v in version:
        if pattern.match(v.text()) is not None:
            version = re.findall(r'Apache Shiro (.*?) is the current stable release', v.text())
            return dict({'ApacheShiro': version[0]})


def ApacheVelocity():
    doc = pyquery.PyQuery(url='https://velocity.apache.org/download.cgi')
    version = doc("a:contains('Velocity Engine')").text()
    return dict({'ApacheVelocity': version})


def ApacheCommonsCollections():
    doc = pyquery.PyQuery(url='http://commons.apache.org/proper/commons-collections/download_collections.cgi')
    div = doc('td.content').children('div.section').items()
    version = {}
    for node in div:
        if str("Apache_Commons_Collections") in node('a').attr('name'):
            key = node('h2 a').attr('name')
            value = node('.a').children('td:first-child').children('a').text()
            version.update({key: value})
    return version


def WildFly():
    doc = pyquery.PyQuery(url='https://www.wildfly.org/downloads/')
    node = doc('div h2').items()
    return dict({'WildFly': [i.text() for i in node]})


def Nginx():
    doc = pyquery.PyQuery(url='https://nginx.org/en/download.html')
    node = doc('td a').items()
    return dict({'Nginx': [i.text() for i in node if 'nginx' in i.text() and 'Windows' not in i.text()]})


def ApacheLog4j():
    doc = pyquery.PyQuery(url='https://logging.apache.org/log4j/2.x/download.html')
    node = doc('tr').find('.externalLink').items()
    return dict({'ApacheLog4j': [i.text() for i in node]})


def FastJson():
    doc = pyquery.PyQuery(url='https://repo1.maven.org/maven2/com/alibaba/fastjson/')
    node = doc('#contents a').items()
    return dict({'FastJson': [i.text() for i in node]})


def ApacheDubbo():
    doc = pyquery.PyQuery(url="https://github.com/apache/dubbo/releases")
    node = doc('.Box-body').find('a:contains(Lastest)').text()
    return

